<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<div id="app">
    <parent></parent>
</div>
</body>

</html>

<!-- 父组件结构 -->
<template id="parent">
    <fieldset>
        <legend>父组件结构</legend>
        <input type="text" v-model="val">
        <child :tochild="val"></child>
    </fieldset>
</template>
<!-- 子组件的结构 -->
<template id="child">
    <fieldset>
        <legend>子组件</legend>
        <button @click="handleClick">点击</button>
        <ul>
            <li v-for="(item,i) in arr" :key="i">{{item}}</li>
        </ul>
    </fieldset>
</template>

<script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script>
<script>
    //父组件

    //回答面试官:父子通信,直接使用props来进行数据传递

    //流程:

    //1.找到子组件的标签,然后动态绑定需要传递的数据

    //2.在子组件的选项中使用props来接收动态绑定的属性的key

    Vue.component('parent', {
        template: "#parent",
        data() {
            return {
                val: ""
            }
        },
        //子组件
        components: {
            child: {
                //porps的属性可以当成和data一样来使用
                props: ['tochild'],
                //事件处理函数
                methods: {
                    handleClick() {
                        this.arr.push(this.tochild)
                    }
                },
                data() {
                    return {
                        arr: ['吃饭']
                    }
                },
                template: "#child"
            }
        }
    })

    let vm = new Vue({
        el: "#app"
    })
</script>